#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <dirent.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>

#include "data_type.h"
#include "cube.h"
#include "cube_define.h"
#include "cube_record.h"


int bubble_trace_init(void * sub_proc,void * para)
{
	int ret;
	// add youself's plugin init func here
	return 0;
}

int bubble_trace_start(void * sub_proc,void * para)
{
	int ret;
	void * recv_msg;
	int i;
	int type;
	int subtype;


	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
				message_get_type(recv_msg),message_get_subtype(recv_msg));
			ex_module_sendmsg(sub_proc,recv_msg);
			continue;
		}
		if((type==TYPE(GENERAL_RETURN))&&(subtype==SUBTYPE(GENERAL_RETURN,STRING)))
		{
			proc_bubble_trace(sub_proc,recv_msg);
		}
		
	}

	return 0;
}

int proc_bubble_trace(void * sub_proc,void * recv_msg)
{
	int ret;
	int i;
	printf("begin proc bubble trace \n");

	int size=10;
	void * new_msg;

	RECORD(GENERAL_RETURN,STRING) * req_data;
	
	ret=message_get_record(recv_msg,&req_data,0);
	
	// convert string to int array and get size value
		//use Getfiledfromstr(char * name,char * str,char IFS,int maxsize) to get int string one by one 
		//IFS equals ',', then use Atoi to change string to int

	// enter bubble sort circle 
	
	// output current result after one step
		// use Itoa to change int to string
		// use Strcat to build whole string
		// at last ,build (GENERAL_RETURN,STRING) record and output it
	{
		new_msg = message_create(TYPE_PAIR(GENERAL_RETURN,STRING),recv_msg);
		message_add_record(new_msg,req_data);
		ex_module_sendmsg(sub_proc,new_msg);
	}

	
	return ret;
}
